package com.itboye.ihomebank.key;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.itboye.ihomebank.R;
import com.itboye.ihomebank.adapter.KaiSuoJiLuAdapter;
import com.itboye.ihomebank.base.BaseOtherActivity;
import com.itboye.ihomebank.bean.KaiSuoJiLu;
import com.itboye.ihomebank.bean.ResultEntity;
import com.itboye.ihomebank.constants.SPContants;
import com.itboye.ihomebank.custom.XListView;
import com.itboye.ihomebank.custom.XListView.IXListViewListener;
import com.itboye.ihomebank.presenter.KeyPresenter;
import com.itboye.ihomebank.util.ByAlert;
import com.itboye.ihomebank.util.SPUtils;

/**
 * 开锁记录
 * 
 */
public class ActivityKaiSuoJiLu extends BaseOtherActivity implements Observer,IXListViewListener {
	XListView jilu_listview;
	ImageView key_title_finish;
	TextView key_title_title;
	RelativeLayout key_title_xuanze;
	TextView key_title_xuan;
	TextView shouquan, dongjie, delete;
	ImageView key_title_shezhi;//下拉的那个框

	private String lockId;//锁id
	private String keyId;//钥匙id
	private String toUid;//钥匙的用户id
	private String status;//锁状态
	private String uid;
	private TextView nodata;
	
	private int page;//页码
	private String pageSize = "10";//每页显示的条数
	
	private List<KaiSuoJiLu.KaiSuoJiLuList> beans=new ArrayList<>();
	KeyPresenter keyPresenter;
	KaiSuoJiLuAdapter adapter;
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.key_title_finish:
			finish();
			break;
		case R.id.key_title_xuanze:// 弹出框

			break;
		case R.id.shouquan:// 授权
			
			break;
		case R.id.dongjie:// 冻结，解冻
			showProgressDialog("修改中,请稍后", true);
			if(dongjie.getText().toString().equals("冻结")){
				keyPresenter.getDongJie(uid, toUid, lockId, keyId);
			}else{
				keyPresenter.getJieDong(uid, toUid, lockId, keyId);
			}
			
			break;
		case R.id.delete:// 删除
			showProgressDialog("修改中,请稍后", true);
			keyPresenter.getShanChu(uid, toUid, lockId, keyId);
			break;
		default:
			break;
		}

	}

	@Override
	protected int layout_id() {
		return R.layout.activity_kaisuo_jilu;
	}

	@Override
	protected void onInitController() {
		jilu_listview.setXListViewListener(this);
		jilu_listview.setPullLoadEnable(true);
		jilu_listview.setPullRefreshEnable(true);
	}

	@Override
	protected void onInit() {
		key_title_title.setText("开锁记录");
		key_title_xuan.setText("全部");
		key_title_shezhi.setVisibility(View.GONE);
		key_title_xuan.setVisibility(View.GONE);
		lockId = getIntent().getStringExtra("lockid");
		keyId = getIntent().getStringExtra("keyid");
		toUid = getIntent().getStringExtra("touid");
		status = getIntent().getStringExtra("status");
		if (status.equals("110405")) {
			dongjie.setText("解冻");
		} else {
			dongjie.setText("冻结");
		}
		
		uid = SPUtils.get(this, null, SPContants.USER_ID, "") + "";
		
		keyPresenter=new KeyPresenter(this);
		page = 1;
		keyPresenter.getKaiSuoJiLu(uid, lockId, page + "", pageSize);
		
		adapter = new KaiSuoJiLuAdapter(this, beans, R.layout.item_kaisuo_jilu);
		jilu_listview.setAdapter(adapter);

	}

	@Override
	protected void onReadIntent(Bundle bundle) {

	}

	@Override
	public void update(Observable observable, Object data) {
		ResultEntity entity = handlerError(data);
		try {
			closeProgressDialog();
		} catch (Exception e) {
		}
		if (entity != null) {
			if (entity.getEventType() == KeyPresenter.kaisuo_success) {
				KaiSuoJiLu bean=(KaiSuoJiLu) entity.getData();
				if (page == 1) {// 当页码数为1时，清空，以免下拉时重复加载数据
					beans.clear();
				}
				
				for (int i = 0; i < bean.getList().size(); i++) {
					if(bean.getList().get(i).getUid().equals(toUid)){
						beans.add(bean.getList().get(i));
					}
				}//循环之后  取出id和上个页面相同的，然后取出来，才是这个id的开锁记录
				
				if (beans.size() <= 0) {
					nodata.setVisibility(View.VISIBLE);
					jilu_listview.setVisibility(View.GONE);
					nodata.setText("该用户暂无开锁记录");
				} else {
					nodata.setVisibility(View.GONE);
					jilu_listview.setVisibility(View.VISIBLE);
					adapter.notifyDataSetChanged();
				}
				try {
					stopLoad();
				} catch (Exception e) {
				}
			}
			if(entity.getEventType() == KeyPresenter.kaisuo_fail){
				try {
					stopLoad();
				} catch (Exception e) {
				}
			}
			//冻结
			if(entity.getEventType() == KeyPresenter.dongjie_success){
				ByAlert.alert(entity.getData());
				dongjie.setText("解冻");
			}
			if(entity.getEventType() == KeyPresenter.dongjie_fail){
				ByAlert.alert(entity.getData());
			}
			//解冻
			if(entity.getEventType() == KeyPresenter.jiedong_success){
				ByAlert.alert(entity.getData());
				dongjie.setText("冻结");
			}
			if(entity.getEventType() == KeyPresenter.jiedong_fail){
				ByAlert.alert(entity.getData());
			}
			//删除
			if(entity.getEventType() == KeyPresenter.shanchu_success){
				ByAlert.alert(entity.getData());
			}
			if(entity.getEventType() == KeyPresenter.shanchu_fail){
				ByAlert.alert(entity.getData());
			}
			
			
		}
	}
	@Override
	public void onRefresh() {
		page = 1;
		keyPresenter = new KeyPresenter(this);
		keyPresenter.getKaiSuoJiLu(uid, lockId, page + "", pageSize);
		
	}

	@Override
	public void onLoadMore() {
		page++;
		keyPresenter = new KeyPresenter(this);
		keyPresenter.getKaiSuoJiLu(uid, lockId, page + "", pageSize);
		
	}
	/**
	 * 停止加载
	 */
	public void stopLoad() {
		jilu_listview.stopLoadMore();
		jilu_listview.stopRefresh();
	}

}
